﻿using PersistenceLayer.DAL;
using SupportLayer.Objetos.Outros;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ByBikeShop.Administracao.Pages
{
	public partial class Preview : System.Web.UI.Page
	{
		public string Imagem { get { return Request.QueryString["Imagem"]; } }
		protected void Page_Load(object sender, EventArgs e)
		{
			if (Imagem != "")
				imgPreview.ImageUrl = ResolveUrl("/Imagens/Banner/" + Imagem);
			else
				Response.Redirect("/Administracao/Pages/Banner.aspx");
		}

		protected void btnSalvar_Click(object sender, EventArgs e)
		{
			try
			{


				var bd = new BannerDAL();
				var lb = bd.Listar();

				PersistenceLayer.Model.Banner b = new PersistenceLayer.Model.Banner();
				b.Imagem_Url = Imagem;

				if ((lb == null) || (lb.Count <= 0))
					b.Ordem = 1;
				else
					b.Ordem = Convert.ToInt16(lb.OrderByDescending(a => a.Ordem).ToList()[0].Ordem + 1);

				bd.Adicionar(b);

				Response.Redirect("/Administracao/Pages/Banner.aspx");
			}
			catch (Exception ex)
			{
				ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "validacao", "alert('" + ex.Message + "');", true);
			}
		}

		protected void btnCancelar_Click(object sender, EventArgs e)
		{
			Response.Redirect("/Administracao/Pages/Banner.aspx");
		}
	}
}